package sort;

import java.util.Random;

/**
 * @author:李雄
 * @ClassName:QuickSort
 * @Description:快速排序
 * @createTime:2022-11-13 20:58
 */
public class QuickSort {

    public static void main(String[] args) {
        int arr[] = new int[10];
        for (int i = 0; i < 10; i++) {
            arr[i] = new Random().nextInt(0 + 10);
        }
        myQuickSort(arr, 0, arr.length - 1);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    private static void myQuickSort(int[] arr, int left, int right) {
        if (arr == null || arr.length == 0) {
            return;
        }
        if (right < left) {
            return;
        }
        int l = left, r = right;
        int temp = arr[left];
        while (r > l) {
            while (r > l) {
                if (arr[r] < temp) {
                    arr[left] = arr[r];
                    arr[r] = temp;
                    break;
                }
                r--;
            }
            while (r > l) {
                if (arr[l] > temp) {
                    arr[r] = arr[l];
                    arr[l] = temp;
                    break;
                }
                l++;
            }
            arr[l] = temp;
        }

        myQuickSort(arr, left, l - 1);
        myQuickSort(arr, l + 1, right);
    }

}
